package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import dto.MaterialOrderQueryDTO;
import dto.SupplierQueryDTO;
import entity.MaterialOrder;
import entity.Supplier;
import lombok.RequiredArgsConstructor;
import org.example.constant.SupervisorConstant;
import org.example.repository.SupervisorRepository;
import org.example.result.PageResult;
import org.example.service.SupervisorService;
import org.springframework.stereotype.Service;
import vo.SupplierListVo;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SupervisorServiceImpl implements SupervisorService {

    private final SupervisorRepository supervisorRepository;

    // ==================== 供应商管理 ====================

    /**
     * 分页查询供应商
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult querySupplier(SupplierQueryDTO queryDTO) {
        // 开始分页查询
        PageHelper.startPage(queryDTO.getPageNumber(), queryDTO.getPageSize());
        Page<Supplier> suppliers = supervisorRepository.querySupplier(queryDTO);
        return new PageResult(suppliers.getTotal(), suppliers.getResult());
    }

    /**
     * 添加供应商
     *
     * @param supplier
     */
    @Override
    public void addSupplier(Supplier supplier) {
        String qualifyRating = passRateCalculation(supplier.getSupplierCount(), supplier.getQualifyCount());
        supplier.setQualityRating(qualifyRating);
        supervisorRepository.addSupplier(supplier);
    }

    /**
     * 更新供应商
     *
     * @param supplier
     */
    @Override
    public void updateSupplier(Supplier supplier) {
        String qualifyRating = passRateCalculation(supplier.getSupplierCount(), supplier.getQualifyCount());
        supplier.setQualityRating(qualifyRating);
        supervisorRepository.updateSupplier(supplier);
    }

    /**
     * 删除供应商
     *
     * @param supplierId
     */
    @Override
    public void deleteSupplier(Integer supplierId) {
        supervisorRepository.deleteSupplier(supplierId);
    }

    // ==================== 原材料订单管理 ====================

    /**
     * 分页查询原材料订单
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult queryMaterialOrder(MaterialOrderQueryDTO queryDTO) {
        // 开始分页查询
        PageHelper.startPage(queryDTO.getPageNumber(), queryDTO.getPageSize());
        Page<MaterialOrder> materialOrders = supervisorRepository.queryMaterialOrder(queryDTO);
        return new PageResult(materialOrders.getTotal(), materialOrders.getResult());
    }

    /**
     * 添加原材料订单
     *
     * @param materialOrder
     */
    @Override
    public void addMaterialOrder(MaterialOrder materialOrder) {
        materialOrder.setOrderStatus(SupervisorConstant.AWAIT_CHECK);
        materialOrder.setCreateTime(LocalDateTime.now());
        materialOrder.setUpdateTime(LocalDateTime.now());
        supervisorRepository.addMaterialOrder(materialOrder);
        Supplier supplier = supervisorRepository.findSupplierById(materialOrder.getSupplierId());
        int randomReduction = new java.util.Random().nextInt(101);
        int qualifyCount = Integer.parseInt(supplier.getQualifyCount());
        int materialCount = Integer.parseInt(materialOrder.getMaterialCount());
        int newQualifyCount = qualifyCount + materialCount - randomReduction;
        supplier.setQualifyCount(String.valueOf(newQualifyCount));
        int currentSupplierCount = Integer.parseInt(supplier.getSupplierCount());
        int newSupplierCount = currentSupplierCount + materialCount;
        supplier.setSupplierCount(String.valueOf(newSupplierCount));
        supervisorRepository.updateSupplier(supplier);
    }

    /**
     * 更新原材料订单
     *
     * @param materialOrder
     */
    @Override
    public void updateMaterialOrder(MaterialOrder materialOrder) {
        materialOrder.setUpdateTime(LocalDateTime.now());
        supervisorRepository.updateMaterialOrder(materialOrder);
    }

    /**
     * 删除原材料订单
     *
     * @param orderId
     */
    @Override
    public void deleteMaterialOrder(Integer orderId) {
        supervisorRepository.deleteMaterialOrder(orderId);
    }

    /**
     * 查询供应商列表
     * @return
     */
    @Override
    public List<SupplierListVo> getSupplierList() {
        return supervisorRepository.supplierList();
    }

    //计算合格率
    private String passRateCalculation(String supplierCount, String qualifyCount) {
        // 计算合格率
        int supplier = Integer.parseInt(supplierCount);
        int qualify = Integer.parseInt(qualifyCount);
        double passRate = (double) qualify / supplier;
        return String.format("%.2f%%", passRate * 100);
    }
}